package com.xtwy.config;

import com.xtwy.interceptor.AuthFilterSecurityInterceptor;
import com.xtwy.service.CustomUserService;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;

/**
 * @作 者: 侯建军
 * @日 期: 2018/7/29 01:24
 * @描 述: springSecurity 配置
 * @公 司：山西迅腾伟业
 */
@Log
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    /**
     * 注入过滤验证拦截器
     */
    @Autowired
    private AuthFilterSecurityInterceptor authFilterSecurityInterceptor;
    /**
     * 注册UserDetailsService 的bean
     * @return
     */
    @Bean
    UserDetailsService customUserService() {
        return new CustomUserService();
    }

    /**
     * user Details Service验证
     * @param auth
     * @throws Exception
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        log.warning("登录验证");
//        auth.userDetailsService(customUserService());
        //注入userDetailsService的实现类
        //解决There is no PasswordEncoder mapped for the id “null”异常
        auth.userDetailsService(customUserService()).passwordEncoder(new BCryptPasswordEncoder());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
//        http.authorizeRequests()
//                .antMatchers("/static/**").permitAll()
//                .anyRequest().authenticated() //任何请求,登录后可以访问
//                .and()
//                .formLogin()
//                .loginPage("/login")
//                .failureUrl("/login?error")
//                .permitAll() //登录页面用户任意访问
//                .and()
//                .logout().permitAll(); //注销行为任意访问
//        //添加拦截器
//        http.addFilterBefore(authFilterSecurityInterceptor, FilterSecurityInterceptor.class);
        http.authorizeRequests()
                .anyRequest().authenticated() //任何请求,登录后可以访问
                .and()
                .formLogin()
                .loginPage("/login")
                .failureUrl("/login?error")
                .permitAll() //登录页面用户任意访问
                .and()
                .logout().permitAll(); //注销行为任意访问
        http.addFilterBefore(authFilterSecurityInterceptor, FilterSecurityInterceptor.class)
                .csrf().disable();

    }
}